
//
// 120615 - AH - Created.
// 120714 - AH - Missing java doc.
// 120728 - AH - Responds with array not list as used to.
//

package org.soekrat.server.servlets.file;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.japril.servlet.HttpRequestDef;
import org.soekrat.server.io.db.DbFile;
import org.soekrat.server.io.db.SoekratDAO;
import org.soekrat.server.io.db.SoekratDAOFactory;
import org.soekrat.server.servlets.AbstractSoekratHttpServlet;

/**
 * <p>
 *   Handles the request to get information on a folder's content.  
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
@SuppressWarnings("serial")
public final class GetFolderContentHttpServlet 
  extends AbstractSoekratHttpServlet 
{
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public GetFolderContentHttpServlet(){}

  @Override
  protected void handleRequest(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException 
  {
    HttpRequestDef def=getHttpRequestDef();
    
    if (!hasParameters(req))
    {
      PrintWriter pw=printSimpleHtmlStart(res);
      pw.println(def.compileApiHtml());
      printSimpleHtmlEnd(pw);
      return;
    }
    
    if (!validate(res, def, req)) return;
    
    long folderId=def.fetch(FOLDER_ID.name(), req).getLongValue();
    
    SoekratDAO dao=new SoekratDAOFactory().create();
    List<DbFile> files=dao.getFolderContent(folderId);
    
    respond(res, files.toArray(DbFile.EMPTY_ARRAY));
  }
  
  @Override
  public HttpRequestDef getHttpRequestDef() 
  {
    return new HttpRequestDef("/soekrat_server/get_folder_content")
      .desc("Get Folder's Content").parameter(FOLDER_ID);
  }
  
}
